package com.example.dell.newstart.my;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.dell.newstart.R;
import com.example.dell.newstart.base.BaseActivity;
import com.example.dell.newstart.bean.MyApp;

import butterknife.ButterKnife;

public class UpdateUserNameInfoActivity extends BaseActivity implements View.OnClickListener {
   // @BindView(R.id.change_name)
   // EditText changeName;
    //@BindView(R.id.change_name_button)
   // ToggleButton changenameButton;
    //@BindView(R.id.et_email)
   // TextView etEmail;
  //  @BindView(R.id.et_phone)
   // TextView etPhone;
     private EditText changename;
   private Button changeNameButton;
   private ImageView mycnameReturn;
    @Override
    public int initLayout() {
        return R.layout.activity_change_name;
    }

    @Override
    public void initView()
    {
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
      // MyApp app = (MyApp) getApplication();//取出我们保存在Application中的用户
      //  User user = app.getCurrentUser();
       changename=findViewById(R.id.change_name);
       changeNameButton=findViewById(R.id.change_name_button);
        mycnameReturn=findViewById(R.id.my_cname_return);
        mycnameReturn.setOnClickListener(this);
       changeNameButton.setOnClickListener(this);
    }

 @Override
 public void onClick(View v) {
     switch (v.getId()){
      case R.id.change_name_button:
         String nameString = changename.getText().toString().trim();
       if (TextUtils.isEmpty(nameString)) {//判断emailString 是不是为空
          toastShort("昵称不能为空");
       }
       else{
           MyApp.getCurrentUser().setUsername(nameString);
           //update(nameString);
          toastShort("修改成功");
          goNextActivity(MyInformationActivity.class);
       }
       break;
         case R.id.my_cname_return:
             finish();
             break;
     }
 }

    //button点击后的逻辑
  /*  @OnClick({ R.id.change_name_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.change_name_button:
              /*  String nameString = changeName.getText().toString().trim();
                // TODO: 2018/12/7 获取到其他修改值，然后调用接口
                StringBuilder stringBuilder = new StringBuilder("以下字段:");
                if (TextUtils.isEmpty(nameString)) {//判断emailString 是不是为空
                    stringBuilder.append(" 昵称");
                }
                String toastString = stringBuilder.toString();
                if (!toastString.equals("以下字段:")) {
                    Toast.makeText(UpdateUserNameInfoActivity.this, toastString + " 不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    //调用后台接口，更新数据
                //    update(nameString);
                }
              toastShort("修改");
                break;
        }
    }*/
    //调用后台接口，更新数据
    //private void update(String nameString) {
    //    Retrofit retrofit = new Retrofit.Builder()
    //            .baseUrl(RequestInterface.basePath)
    //            .addConverterFactory(GsonConverterFactory.create())
    //            .build();

    //    RequestInterface.UpdateUserInfo updateUserInfo = retrofit.create(RequestInterface.UpdateUserInfo.class);

      // Call<ServerResponse<User>> call = updateUserInfo.update(nameString);

     /*  call.enqueue(new Callback<ServerResponse<User>>() {

            @Override
            public void onResponse(Call<ServerResponse<User>> call, Response<ServerResponse<User>> response) {
                if (response.body() == null) {
                    toastShort("获取后台数据失败,请检查网络和服务器情况！");
                    return;
                }
                if (response.body().getStatus() == 0) {
                    //获取返回的数据（就是泛型ServerResponse中的T所代表的数据）
                    User user = response.body().getData();
                    //获取当前应用
                    MyApp app = (MyApp) getApplication();
                    //把返回的user保存到myAPP中
                    app.setCurrentUser(user);
                    Log.d(TAG, "onResponse: user^^^^^^^^^^" + user.toString());
                    Toast.makeText(UpdateUserNameInfoActivity.this, "更新信息成功", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(UpdateUserNameInfoActivity.this, "更新信息失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ServerResponse<User>> call, Throwable t) {
                Log.d(TAG, "onFailure: ");
                Toast.makeText(UpdateUserNameInfoActivity.this, "网络请求错误" + t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });*/
 }
